/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Tree;

import java.io.File;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Alex
 */
public class FileNode {
    
    protected File _file;
    
    /**
     * 
     * @param file 
     */
    public FileNode(File file)
    {
       _file = file; 
    }
    
    /**
     * 
     * @return 
     */
    public File getFile()
    {
        return _file;
    }
    
    
    /**
     * 
     * @return 
     */
    @Override
    public String toString()
    {
        return _file.getName().length() > 0 ? _file.getName() : _file.getPath();
    }
    
    
    public boolean expand(DefaultMutableTreeNode parent)
    {
        DefaultMutableTreeNode flag = (DefaultMutableTreeNode) parent.getFirstChild();
        
        // No child
        if (flag == null)
        {
            return false;
        }
        else // has child
        {
            Object obj = flag.getUserObject();
            if(!(obj instanceof Boolean))
            {
                return false; // already expanded
            }
            
            parent.removeAllChildren(); // Remove flag
            
            File[] files = listFiles();
            
            if (files == null)
            {
                return true;
            }
            
            // Push files into vector, before pack into FileNode
            Vector fileVector = new Vector();
            
            int index2, index3;
            boolean isAdded;
            
            for (index2 = 0; index2 < files.length; index2++)
            {
                File f = files[index2];
                if(!f.isDirectory())
                {
                    continue;
                }
                
                FileNode newNode = new FileNode(f);
                isAdded = false;
                
                // Sort abc
                for (index3 = 0; index3 < fileVector.size(); index3++)
                {
                    FileNode nd = (FileNode) fileVector.elementAt(index3);
                    if (newNode.compareTo(nd) < 0)
                    {
                        fileVector.insertElementAt(newNode, index3);
                        isAdded = true;
                    }
                }
                
                if (!isAdded)
                {
                    fileVector.addElement(newNode);
                }
            }
            
            int index4;
            
            // Pack FileNodes into IconData, and then pack IconData into DefaultMutableTreeNode
            for (index4 = 0; index4 < fileVector.size(); index4++)
            {
                FileNode nd = (FileNode) fileVector.elementAt(index4);
                IconData idata = new IconData(null, null, nd);
                DefaultMutableTreeNode node =
                        new DefaultMutableTreeNode(idata);

                //add node cờ flag kiểu Boolean vào node con 
                if (nd.hasSubDirs())
                {
                    node.add(new DefaultMutableTreeNode(
                            new Boolean(true)));
                }

                parent.add(node);            
            }
            return true;
            
        }
        
        
    }
    
    /**
     * 
     * @return 
     */
    public Boolean hasSubDirs()
    {
        File[] files = listFiles();
        if(files == null)
        {
            return false;
        }
        
        int index;
        
        for (index = 0; index < files.length; index++)
        {
            if(files[index].isDirectory())
            {
                return true;
            }
        }
        return false;
    }
    
    /***
     * 
     * @param compareNode
     * @return 
     */
    public int compareTo(FileNode compareNode)
    {
        return _file.getName().compareToIgnoreCase(compareNode._file.getName());
    }
    
    protected File[] listFiles()
    {
        if (!_file.isDirectory()) // is single file
        {
            return null;
        }
        else // is directory
        {
            try{
                return _file.listFiles();
            }catch(Exception ex){
                ex.printStackTrace();
                return null;
            }
        }
    }
}
